feat: add CNS (Cartão Nacional de Saúde) validator - #775
Open
laurazimrn wants to merge 1 commit into
Open
Conversation
Closes brazilian-utils#774 Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #775 +/- ##
==========================================
+ Coverage 99.09% 99.14% +0.04%
==========================================
Files 26 27 +1
Lines 775 820 +45
==========================================
+ Hits 768 813 +45
Misses 7 7 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
Adds first-class support for validating, generating, formatting, and de-formatting Brazilian CNS (Cartão Nacional de Saúde) numbers, aligning CNS functionality with existing document utilities in brutils.
Changes:
- Introduces
brutils/cns.pyimplementing CNS validation (definitive + provisional), generation, formatting, and symbol removal. - Adds a dedicated CNS test suite and wires the new utilities into the package root exports.
- Updates documentation (PT/EN) and the changelog to reflect the new CNS APIs.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| brutils/cns.py | New CNS core implementation: validate/generate/format/remove symbols. |
| brutils/init.py | Re-exports CNS utilities via package root (is_valid_cns, generate_cns, etc.). |
| tests/test_cns.py | Adds coverage for CNS validation, generation, formatting, and symbol removal. |
| README.md | Documents CNS utilities in Portuguese and updates TOC. |
| README_EN.md | Documents CNS utilities in English and updates TOC. |
| CHANGELOG.md | Records newly added CNS utilities under Unreleased. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+23
to
+40
| """ | ||
| Remove formatting symbols from a CNS. | ||
|
|
||
| This function takes a CNS (Cartão Nacional de Saúde) string with | ||
| formatting symbols and returns a cleaned version with no symbols. | ||
|
|
||
| Args: | ||
| cns (str): A CNS string that may contain formatting symbols. | ||
|
|
||
| Returns: | ||
| str: A cleaned CNS string with no formatting symbols. | ||
|
|
||
| Example: | ||
| >>> remove_symbols("898 0032 6314 4970") | ||
| '898003263144970' | ||
| >>> remove_symbols("898003263144970") | ||
| '898003263144970' | ||
| """ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Descrição
Adiciona suporte ao CNS (Cartão Nacional de Saúde), também conhecido como "Cartão do SUS", seguindo o mesmo padrão dos módulos existentes (ex:
pis.py,voter_id.py).Mudanças Propostas
brutils/cns.pycom:is_valid_cns: valida um CNS de 15 dígitos, cobrindo os dois formatos oficiais — definitivo (inicia com 1 ou 2) e provisório (inicia com 7, 8 ou 9) — via dígito verificador módulo 11.generate_cns: gera um CNS válido aleatório (definitivo por padrão, ou provisório viais_final=False).format_cns: formata um CNS válido para exibição (161 2433 7445 0004).remove_symbols_cns: remove símbolos de formatação.tests/test_cns.py.brutils/__init__.py.README.mdeREADME_EN.md.CHANGELOG.md.O algoritmo foi validado por fuzzing (50k+ casos) contra uma implementação de referência do CNS, além de 10k gerações de cada tipo (definitivo/provisório) confirmadas como válidas.
Checklist de Revisão
Declaração de Uso de IA (OBRIGATÓRIA)
Ferramenta usada: Claude Code (Anthropic). Revisei o código gerado, rodei
ruff format/ruff checke a suíte de testes completa (183 testes, todos passando), e validei o algoritmo de dígito verificador do CNS por fuzzing contra uma implementação de referência independente antes de submeter.Comentários Adicionais (opcional)
Issue Relacionada
Closes #774